package edu.tongji.app.adaptor;

import java.util.ArrayList;

import edu.tongji.app.R;
import edu.tongji.app.component.STHeitiLightTextView;
import edu.tongji.app.model.NewsFeedItem;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;

public class NewsFeedListAdapter extends BaseAdapter {

	private ArrayList<NewsFeedItem> newsFeedList;
	private Context appContext;
	private Context activityContext;

	public NewsFeedListAdapter(Context appContext, Context activityContext,
			ArrayList<NewsFeedItem> newsFeedList) {
		this.setAppContext(appContext);
		this.setActivityContext(activityContext);
		this.newsFeedList = newsFeedList;
	}

	public Context getAppContext() {
		return appContext;
	}

	public void setAppContext(Context appContext) {
		this.appContext = appContext;
	}

	public Context getActivityContext() {
		return activityContext;
	}

	public void setActivityContext(Context activityContext) {
		this.activityContext = activityContext;
	}

	public ArrayList<NewsFeedItem> getNewsFeedList() {
		return newsFeedList;
	}

	public void setNewsFeedList(ArrayList<NewsFeedItem> newsFeedList) {
		this.newsFeedList = newsFeedList;
	}

	@Override
	public int getCount() {
		return newsFeedList.size();
	}

	@Override
	public Object getItem(int position) {
		return newsFeedList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder = new Holder();

		NewsFeedItem feedItem = newsFeedList.get(position);
		String color = feedItem.getHexColor();
		String date = feedItem.getDate();
		String content = feedItem.getContent();

		if (convertView == null) {

			LayoutInflater inflater = (LayoutInflater) appContext
					.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.item_news_feed, null);
			STHeitiLightTextView dateView = (STHeitiLightTextView) convertView
					.findViewById(R.id.news_date_view);

			STHeitiLightTextView contentView = (STHeitiLightTextView) convertView
					.findViewById(R.id.news_content_view);

			RelativeLayout newsColorLayout = (RelativeLayout) convertView
					.findViewById(R.id.news_color_view);

			holder.setDateView(dateView);
			holder.setNewsColorLayout(newsColorLayout);
			holder.setContentView(contentView);
			holder.setColor(color);

			convertView.setTag(holder);

		} else {
			holder = (Holder) convertView.getTag();
		}

		Drawable drawable = null;
		if (color.equals("42c3f7")) {
			drawable = activityContext.getResources().getDrawable(
					R.drawable.corners_3px_42c3f7);
		} else if (color.equals("ff634d")) {
			drawable = activityContext.getResources().getDrawable(
					R.drawable.corners_3px_ff634d);
		} else if (color.equals("d156ff")) {
			drawable = activityContext.getResources().getDrawable(
					R.drawable.corners_3px_d156ff);
		} else if (color.equals("abe631")) {
			drawable = activityContext.getResources().getDrawable(
					R.drawable.corners_3px_abe631);
		}
		
		holder.getDateView().setText(date);
		holder.getContentView().setText(content);
		holder.getNewsColorLayout().setBackground(drawable);

		return convertView;
	}

	class Holder {

		private STHeitiLightTextView dateView;
		private STHeitiLightTextView contentView;
		private RelativeLayout newsColorLayout;

		private String color;

		public STHeitiLightTextView getDateView() {
			return dateView;
		}

		public void setDateView(STHeitiLightTextView dateView) {
			this.dateView = dateView;
		}

		public STHeitiLightTextView getContentView() {
			return contentView;
		}

		public void setContentView(STHeitiLightTextView contentView) {
			this.contentView = contentView;
		}

		public RelativeLayout getNewsColorLayout() {
			return newsColorLayout;
		}

		public void setNewsColorLayout(RelativeLayout newsColorLayout) {
			this.newsColorLayout = newsColorLayout;
		}

		public String getColor() {
			return color;
		}

		public void setColor(String color) {
			this.color = color;
		}

	}
}
